package com.dhcc.android.base.base;

import android.app.Activity;
import android.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.dhcc.android.base.utils.CrashHandler;

import java.util.LinkedList;
import java.util.List;

/**
 * ITSMOB 全局上下文
 *
 * @author pengling
 * @date 2014年8月29日
 */
public class MSApplication extends Application {

    /**
     * 恢复
     */
    public static final String USERNAME = "username";
    public static final String PASSWORD = "password";
    public static final String PERIOD_CODE = "period_code";
    public static final String PERIOD_LABEL = "period_label";
    public static final String TABLE_CODE = "table_code";
    public static final String TABLE_LABEL = "table_label";
    public static final String TASK = "task";
    public static final String EXCEPTION_FLAG = "exception_flag";

    /**
     * 系统设置
     */
    public static final String TASK_SAVE_PATH = "task_save_path";
    public static final String GPS_REFRESH_TIME = "gps_refresh_time";
    public static final String NOTE_DISTANCE = "note_distance";
    public static final String CATCH_RANGE = "catch_range";
    public static final String DK_TZ = "dk_tz";
    public static final String DK_HB = "dk_hb";
    public static final String DK_CF = "dk_cf";
    public static final String DK_SC = "dk_sc";

	/*
     * Constants for PREFERENCE_KEY The format of the Preference key is :
	 * $name_KEY = "$name"
	 */
    /**
     * Preference key for account isautologin.
     */
    public static final String ACCOUNT_ISAUTOLOGIN_KEY = "account_isautologin";
    /**
     * Preference key for account username.
     */
    public static final String ACCOUNT_USERNAME_KEY = "account_username";
    /**
     * Preference key for account password.
     */
    public static final String ACCOUNT_PASSWORD_KEY = "account_password";
    /**
     * Preference key for firstloginshowflag.
     */
    public static final String FISRTLOGINSHOWFLAG = "firstloginshowflag";
    /**
     * Preference key for status (available, busy, away, ...).
     */
    public static final String STATUS_KEY = "status";
    /**
     * Preference key for status message.
     */
    public static final String STATUS_TEXT_KEY = "status_text";
    /**
     * Preference key for link src
     */
    public static final String LINK_SRC = "link_sec";
    /**
     * Preference key for link src
     */
    public static final String CURRENT_IP = "currentIp";
    /**
     * Preference key for link src
     */
    public static final String CURRENT_PORT = "currentPort";
    public static final String CONFIG_HISTORY = "config_history";
    public static final String LOGIN_HISTORY = "login_history";
    public static final String IP_HISTORY = "ip_history";
    public static final String PORT_HISTORY = "port_history";
    public static final String SEARCH_KNOW_HISTORY = "search_know_history";
    public static final String SEARCH_CI_HISTORY = "search_ci_history";

    public static final String EXITACTIVITY = "search_ci_history";

    private boolean mIsConnected;
    private boolean mIsAccountConfigured;
    private boolean mPepEnabled;
    private final PreferenceListener mPreferenceListener = new PreferenceListener();
    private SharedPreferences mSettings;
    public static String login;
    private static MSApplication mApplication;

    @Override
    public void onCreate() {
        super.onCreate();
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(getApplicationContext());
        mSettings = PreferenceManager.getDefaultSharedPreferences(this);
        login = mSettings.getString(MSApplication.ACCOUNT_USERNAME_KEY, "");
        String password = mSettings.getString(
                MSApplication.ACCOUNT_PASSWORD_KEY, "");
        mIsAccountConfigured = !("".equals(login) || "".equals(password));
        mSettings.registerOnSharedPreferenceChangeListener(mPreferenceListener);
        mApplication = this;
    }

    public static MSApplication instance() {
        return mApplication;
    }

    private List<Activity> mList = new LinkedList<Activity>();

    public List<Activity> getmList() {
        return mList;
    }

    // add Activity
    public void addActivity(Activity activity) {
        mList.add(activity);
    }

    public void deleteActivity(Activity activity) {
        mList.remove(activity);
    }

    // 关闭每一个list内的activity
    public void exit() {
        try {
            for (Activity activity : mList) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }

    // 杀进程
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        mSettings
                .unregisterOnSharedPreferenceChangeListener(mPreferenceListener);
    }

    /**
     * Tell if Beem is connected to a XMPP server.
     *
     * @return false if not connected.
     */
    public boolean isConnected() {
        return mIsConnected;
    }

    /**
     * Set the status of the connection to a XMPP server of BEEM.
     *
     * @param isConnected set for the state of the connection.
     */
    public void setConnected(boolean isConnected) {
        mIsConnected = isConnected;
    }

    /**
     * Tell if a XMPP account is configured.
     *
     * @return false if there is no account configured.
     */
    public boolean isAccountConfigured() {
        return mIsAccountConfigured;
    }

    /**
     * Enable Pep in the application context.
     *
     * @param enabled true to enable pep
     */
    public void setPepEnabled(boolean enabled) {
        mPepEnabled = enabled;
    }

    /**
     * Check if Pep is enabled.
     *
     * @return true if enabled
     */
    public boolean isPepEnabled() {
        return mPepEnabled;
    }

    /**
     * A listener for all the change in the preference file. It is used to
     * maintain the global state of the application.
     */
    private class PreferenceListener implements
            SharedPreferences.OnSharedPreferenceChangeListener {

        /**
         * Constructor.
         */
        public PreferenceListener() {
        }

        @Override
        public void onSharedPreferenceChanged(
                SharedPreferences sharedPreferences, String key) {
            if (MSApplication.ACCOUNT_USERNAME_KEY.equals(key)
                    || MSApplication.ACCOUNT_PASSWORD_KEY.equals(key)) {
                String login = mSettings.getString(
                        MSApplication.ACCOUNT_USERNAME_KEY, "");
                String password = mSettings.getString(
                        MSApplication.ACCOUNT_PASSWORD_KEY, "");
                mIsAccountConfigured = !("".equals(login) || ""
                        .equals(password));
            }
        }
    }
}
